/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_ora_detect.c                                    */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/21 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>
#include <sys/time.h>

#include "frcomm.h"
#include "frmem.h"

#include "../../../src/module/oracle/config.h"
#include "../test_ora_comm.h"
#include <oracle/ora_core.h>
#include <frct_comm.h>
#include <apmtest_comm.h>

extern int ora_detect(Conntrack *p_st_conn
                    , Apm_packet *p_st_packet);

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

/**
 * 正常测试
 */
static void __case1(void)
{
    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_ora_create_ct(&p_st_conn, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT), FR_SUCC);

    char *p_bin = (char *) "00e7000001000000013b012c0c412000ffff4f980000000100a10046000000008181000000000000000000000000000000000000000000000000000020000020000000000000284445534352495054494f4e3d28414444524553533d2850524f544f434f4c3d5443502928484f53543d3137322e31362e302e37372928504f52543d31353231292928434f4e4e4543545f444154413d284349443d2850524f4752414d3d4a444243205468696e20436c69656e742928484f53543d5f5f6a6462635f5f2928555345523d57696e292928534552564943455f4e414d453d7064626f72636c292929";
    char arr_payload[65536] = {'\0'};
    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);
    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT, st_timestamp), FR_SUCC);

    CU_ASSERT_EQUAL(ora_detect(p_st_conn, p_st_packet), APM_L7_IS_YES);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);
    apmtest_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

int add_suite_ora_detect(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" ora_detect()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
